<?php

/**
 *  角色控制器
 * @author xiaojy  xiaojy@wanthings.com
 * @copyright 成都网信天成科技有限公司, 2017, http://www.wanthings.com
 */
class RoleController extends Controller
{

    public $admin_name = '';
    public $sp = '';
    public $user_sp = '';

    public function options()
    {
        return Role::model()->sp();
    }

    public function actionList()
    {
        $this->pageTitle = '角色配置';
        $this->active_nav = 'setting';
        $this->active_nav2 = 'tms_user';
        $this->active_nav3 = 'role';
        $filter = array();
        $filter['name'] = Yii::app()->request->getQuery('name');
        $filter['page'] = Yii::app()->request->getQuery('page');
        $filter['page_size'] = Yii::app()->request->getQuery('page_size', 10);
        //角色所属
        //$filter['carry_id'] = $this->company_id;

        $filter['default_role'] = 1;    //包括管理员设定的权限
        $data = Role::model()->roles($filter);
        $pager = $data['pager'];
        $this->setPageTitle('平台角色列表');
        $this->render('list_role', array('data' => $data, 'filter' => $filter, 'pager' => $pager));
    }


    public function actionAdd()
    {
        $this->pageTitle = '角色配置';
        $this->active_nav = 'setting';
        $this->active_nav2 = 'tms_user';
        $this->active_nav3 = 'role';
        if (Yii::app()->request->isPostRequest) {
            $role = Yii::app()->request->getPost('role');
            $role['carry_id'] = $this->company_id;
            $id = Role::model()->addRole($role);
            if ($id > 0) {
                $this->showSuccess('添加成功', $this->createUrl('role/list'));
            } else {
                $this->showMessage('添加失败');
                if ($id == Role::ADD_ERROR_USING_NAME) {
                    $this->showMessage('该名称已经使用');
                }
            }
            // $this->redirect($this->createUrl('role/list'));
        }
        $type = 4;
        $options = Role::model()->sp($type);
        if ($this->is_driver > 0) {
            $type = 3;//司机权限
            $options = Role::model()->sp($type);
        }
        if ($this->shipper_type > 0) {
            $type = 2;//货主权限
            $options = Role::model()->sp($type);
        }
        if ($this->carry_type > 0) {
            $temp = array();
            $type = 1; //承运人权限
            if ($this->shipper_type > 0) {
                $temp = $options;
            }
            $options = Role::model()->sp($type);
            foreach ($temp as $key => $value) {
                if (isset($options[$key])) {
                    $options[$key] = array_merge($options[$key], $value);
                } else {
                    $options[$key] = $value;
                }
            }
        }

        $this->setPageTitle('添加账号');
        $this->render('add', compact('options'));
    }


    public function actionAddDef()
    {
        $this->pageTitle = '角色配置';

        if (Yii::app()->request->isPostRequest) {
            $role = Yii::app()->request->getPost('role');
            $id = Role::model()->addRole($role);
            if ($id > 0) {
                $this->showSuccess('添加成功', $this->createUrl('role/list'));
            } else {
                $this->showMessage('添加失败');
                if ($id == Role::ADD_ERROR_USING_NAME) {
                    $this->showMessage('该名称已经使用');
                }
            }

        }
        $type = 2;
        $options = Role::model()->sp($type);

        $temp = array();
        $type = 1; //承运人权限
        if ($this->shipper_type > 0) {
            $temp = $options;
        }
        $options = Role::model()->sp($type);
        foreach ($temp as $key => $value) {
            if (isset($options[$key])) {
                $options[$key] = array_merge($options[$key], $value);
            } else {
                $options[$key] = $value;
            }
        }

        $this->setPageTitle('添加默认账号');
        $this->render('add', compact('options'));
    }


    public function actionDisable()
    {
        $id = Yii::app()->request->getQuery('id');
        $status = Role::model()->changeStatus($id, 'disable');
        if ($status > 0) {
            $this->showSuccess('已禁用');
        } else {
            $this->showMessage('禁用失败');
        }
        $this->redirect($this->createUrl('role/list'));
    }

    public function actionEnable()
    {
        $id = Yii::app()->request->getQuery('id');
        $status = Role::model()->changeStatus($id, 'enable');
        if ($status > 0) {
            $this->showSuccess('已启用');
        } else {
            $this->showMessage('启用失败');
        }
        $this->redirect($this->createUrl('role/list'));
    }

    public function actionDelete()
    {
        $id = Yii::app()->request->getQuery('id');
        $carry_id = $this->company_id;
        $status = Role::model()->deleteById($id, $carry_id);
        if ($status) {
            $this->showSuccess('删除成功');
        } else {
            $this->showMessage('删除失败');
        }

        $this->redirect($this->createUrl('role/list'));
    }

    public function actionEdit()
    {
        $this->pageTitle = '角色配置';
        $this->active_nav = 'setting';
        $this->active_nav2 = 'tms_user';
        $this->active_nav3 = 'role';
        $operation_id = $this->company_id;
        if (Yii::app()->request->isPostRequest) {
            $role = Yii::app()->request->getPost('role');
            $result = Role::model()->editAndUser($operation_id, $role);
            if ($result['errno'] == 0) {
                $this->showSuccess('修改成功', $this->createUrl('/tms/role/list'));
                return;
            } else {
                $this->showMessage('修改失败' . $result['errmsg']);
            }
            $this->redirect(array('role/edit/id/' . $role['id']));
        }
        $id = Yii::app()->request->getQuery('id');
        $role = Role::model()->getArrById($id);
        $type = 4;
        $options = Role::model()->sp($type);
        if ($this->is_driver > 0) {
            $type = 3;//司机权限
            $options = Role::model()->sp($type);
        }
        if ($this->shipper_type > 0) {
            $type = 2;//货主权限
            $options = Role::model()->sp($type);
        }
        if ($this->carry_type > 0) {
            $temp = array();
            $type = 1; //承运人权限
            if ($this->shipper_type > 0) {
                $temp = $options;
            }
            $options = Role::model()->sp($type);
            foreach ($temp as $key => $value) {
                if (isset($options[$key])) {
                    $options[$key] = array_merge($options[$key], $value);
                } else {
                    $options[$key] = $value;
                }
            }

        }

        $this->render('edit', compact('role', 'options'));
    }

    public function actionLook()
    {
        $this->pageTitle = '角色配置';
        $this->active_nav = 'setting';
        $this->active_nav2 = 'tms_user';
        $this->active_nav3 = 'role';
        $id = Yii::app()->request->getQuery('id');
        $role = Role::model()->getArrById($id);
        $type = 4;
        if ($this->shipper_type > 0) {
            $type = 2;//货主权限
            $options = Role::model()->sp($type);
        }
        if ($this->carry_type > 0) {
            $temp = array();
            $type = 1; //承运人权限
            if ($this->shipper_type > 0) {
                $temp = $options;
            }
            $options = Role::model()->sp($type);
            foreach ($temp as $key => $value) {
                if (isset($options[$key])) {
                    $options[$key] = array_merge($options[$key], $value);
                } else {
                    $options[$key] = $value;
                }
            }

        }

        $this->render('look', compact('role', 'options'));
    }

    //角色测试,权限与名称
    public function actionRoleTest()
    {
        $ids = Yii::app()->request->getQuery('id', array());
        $sp = array();
        $names = array();
        foreach ($ids as $key => $value) {
            $temp = array();
            $role = Role::model()->getArrById($value);
            if ($role['status'] == Role::MAN_STATUS_DISABLE) {
                //被禁用的角色权限取消
                continue;
            }
            $temp = $role['sp'];
            $names[] = $role['name'];
            $sp = array_merge($sp, $temp);
        }
        print_r($sp);
        print_r($names);
    }

    public function actionTest()
    {
        $roles = $this->roles;
        $sp = $this->sp;
        print_r($roles);
        echo '<br>';
        print_r($sp);
    }

}